package com.zz.service;

import com.spring.BeanPostProcessor;
import com.spring.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

@Component
public class ZzpostProcess implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        if(beanName.equals("userService")){
            ((UserServiceImpl)bean).setBeanName("用户服务类");
        }
        System.out.println(beanName+"初始化前-----------------------");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) {
        if(beanName.equals("userservice")){
            Object proxyInstance=Proxy.newProxyInstance(ZzpostProcess.class.getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    System.out.println("进行代理");
                    return method.invoke(bean,args);
                }
            });
           return proxyInstance;
        }
        System.out.println(beanName+"初始化后-----------------------");
        return bean;
    }
}
